package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.EmpMapper;
import com.example.mapper.RoleMapper;
import com.example.pojo.Emp;
import com.example.pojo.Role;
import com.example.service.IEmpService;
import com.example.util.JwtTokenUtil;
import com.example.vo.ResultVo;
import com.example.vo.respvo.EmpVo;
import com.example.vo.respvo.Option;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-11
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private EmpMapper em;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public PageInfo<Emp> getEmpPageInfo(Integer pageNum, Integer pageSize, String empName, String workId, Integer positionId, Integer deptId, String companyName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Emp> emps = baseMapper.selectPageInfo(empName,workId,positionId,deptId,companyName);
        return new PageInfo<>(emps);
    }

    @Override
    public ResultVo getEmpsByDeptId(Integer deptId) {
//        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
//        wrapper.eq("dept_id",deptId);
        List<Emp> list = baseMapper.selectEmpsByDeptId(deptId);
        return ResultVo.response(list);
    }

    @Override
    public Emp getEmpByUserName(String username) {
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id",username);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<Role> getRolesByEmpId(Integer empId) {
        return roleMapper.selectRolesByEmpId(empId);
    }

    @Override
    public List<EmpVo> byWorkSceduleList(Integer deptId) {
        return em.selectByWorkScedule(deptId);
    }

    @Override
    public ResultVo replacementUserPwd(Emp emp) {
        String pwd = emp.getPassword();
        if(pwd==null || pwd.length() < 5){
            return ResultVo.error("密码不能为空且长度不能小于5");
        }
        String encodePwd = passwordEncoder.encode(emp.getPassword());
        emp.setPassword(encodePwd);
        int i = baseMapper.updateById(emp);
        return i > 0 ? ResultVo.ok() : ResultVo.error("密码修改失败！");
    }

    @Override
    public ResultVo updateUserPwd(HttpServletRequest request, String oldPwd, String newPwd) {
        String userId = jwtTokenUtil.getUserIdFormToken(request);
        Emp emp = baseMapper.selectOne(new QueryWrapper<Emp>().eq("id", userId).eq("password", passwordEncoder.encode(oldPwd)));
        if(emp==null){
            return ResultVo.error("用户密码错误");
        }
        Emp newEmp = new Emp().setPassword(passwordEncoder.encode(newPwd)).setId(emp.getId());
        int i = baseMapper.updateById(newEmp);
        return i > 0 ? ResultVo.ok() : ResultVo.error("修改失败");
    }

    @Override
    public List<Emp> getDeptEmp(@Param("dept_name") String deptName) {
        return baseMapper.getDeptEmp(deptName);
    }

    @Override
    public ResultVo getEmpOptions() {
        List<Option> list= baseMapper.selectEmpOptions();
        return  ResultVo.response(list);
    }
}
